<?php
session_start();

require_once("../database.include.php");

	$username=$_GET["username"];
				
    $sql = "SELECT `name`, `description` FROM `group` 
    WHERE `name` IN (SELECT `group_name` FROM `group_user` WHERE `user_name` = '" . $username . "')
    ORDER BY `name`";
    
    $stmt = $conn->prepare($sql);
	$stmt->execute();
	$count = $stmt->rowcount();
    
    if ($count > 0) {
	
   	    echo "<fieldset><legend>Please select a group and an operation.</legend>";
   	    echo "<input type='hidden' id='groupSelected' value=''>";
		echo "<table border='0' style='border-style:none;'><tr><td style='border-style:none;'>";
	    echo "<table border='1' style='vertical-align:text-top;'><tr><td>&nbsp;</td><td>Group Name</td><td>Group Description</td></tr>";
	    $ix = 1;
		foreach ($conn->query($sql) as $row) {
	    	echo "<tr>";
	    	echo "<td><input type='radio' name='groups' id='groups' value='" . $row['name'] . "' 
	    		onclick=document.getElementById('groupSelected').value='" . $row['name'] . "'>";
			echo "</td>";
	        echo "<td>" . $row['name'] . "</td>";
	        echo "<td>" . $row['description'] . "</td>";
	        echo "</tr>";
			$ix++;
	    }
	    echo "</table>";
		echo "</td><td style='border-style:none;'>";
		echo "<center><b>Operations</b></center><br/>";
		echo "<button onclick='showGroupMessages();'>Show Messages</button><br/>";
		echo "<button onclick='sendGroupMessage();'>Send Message</button><br/>";
		echo "<button onclick='showGroupUsers();'>List Users</button><br/>";
	    echo "<button onclick='showInviteUser();'>Invite User</button><br/>";
		echo "<button onclick='createGroup();'>New Group</button><br/>";
		echo "<button onclick='deleteGroup();'>Delete Group</button><br/>";
		echo "<button onclick='leaveGroup();'>Leave Group</button><br/>";
		echo "</fieldset>";
		echo "</td></tr></table>";
	} else { 
		echo "<p>Now would be a great time to start a Privr Group! <a href='#' onclick='createGroup()'>Create a New Group</a></p><br>";
	}
?>
